#include "CStateMainMenu.h"

#include "CBattle.h"
#include "CMinionNode.h"
#include "COverlord.h"
#include "CStateGame.h"
#include "CstateRoom.h"
#include "CStateGraveyard.h"
#include "CNetworkManager.h"
#include "CLabelAdder.h"
#include "CChatBox.h"
#include "CGUIGame.h"

CStateMainMenu::CStateMainMenu(CGameManager * mgr) : CState(mgr)
{

}

CStateMainMenu::~CStateMainMenu()
{
  parent->remove();
}


void CStateMainMenu::init()
{
  parent = env->addTab (rect<s32>(0, 0, 800, 600));

  //env->addImage(driver->getTexture("Dracula.jpg"), position2d<s32> (0,0), true, parent, -1, L"Noob");
  IGUIImage* background = env->addImage( rect<s32>(0,0,SCREEN_W, SCREEN_H), parent, -1, L"L.O.N.");
  background->setImage(driver->getTexture("front2.png"));
  background->setScaleImage(true);

 IGUIImage* background2 = env->addImage( rect<s32>(0,0,800,40), parent, -1, L"L.O.N.");
 background2->setImage(driver->getTexture("baseline.jpg"));
 background2->setScaleImage(true);

 IGUIImage* background3 = env->addImage( rect<s32>(0,560,800,600), parent, -1, L"L.O.N.");
 background3->setImage(driver->getTexture("lowbaseline.jpg"));
 background3->setScaleImage(true);


  IGUIImage * bg= env->addImage( rect<s32>(600, 0, 800, 600), parent);
  bg->setImage(driver->getTexture("sidebar.png"));
  bg->setScaleImage(true);
  bg->setUseAlphaChannel(true);

 env->addButton(rect<s32>(620, 370, 780, 395), parent, BTN_CREA_GAME, L"Create Room");
 env->addStaticText(L"IP: ", rect<s32>(620, 410, 640, 435), false, false, parent);
 txtIpAddress = env->addEditBox(L"", rect<s32>(640, 410, 780, 435), true, parent, TXT_IP_ADDRESS);
 env->addButton(rect<s32>(620, 435, 780, 460), parent, BTN_JOIN_GAME, L"Join Room");
 env->addButton(rect<s32>(620, 475, 780, 500), parent, BTN_TEMPO_BTN, L"Tempo Button");
 env->addButton(rect<s32>(620, 515, 780, 540), parent, BTN_GRAVEYARD, L"Graveyard");
 env->addButton(rect<s32>(620, 555, 780, 580), parent, BTN_EXIT_GAME, L"Log Out");

 //! OVERLORD DETAILS
  COverlord *player = mgr->getPlayer();
  stringw type, title;
  switch (player->getType())
  {
    case OLT_ATTACKER: type = L"Attacker"; break;
    case OLT_DEFENDER: type = L"Defender"; break;
    case OLT_CASTER:   type = L"Caster";   break;
    default: type = L"Unknown"; break;
  }

  switch (player->getTitle())
  {
    case ORT_NEWCOMER:    title = L"Newcomer";     break;
    case ORT_CHAMPION:    title = L"Champion";     break;
    case ORT_VANQUISHER:  title = L"Vanquisher";   break;
    case ORT_FALLEN:      title = L"Fallen";       break;
    case ORT_NETHER_LORD: title = L"Nether Lord";  break;
    default: type = L"Unknown"; break;
  }

  IGUIStaticText *olFrame = env->addStaticText(L"", rect<s32>(620, 20, 780, 210), true, true, parent);
  CLabelAdder adder(env, olFrame, 5, 25, 50, 130);
  env->addStaticText(L"Overlord Details", rect<s32>(5, 5, 180, 25), false, false, olFrame);
  adder.addLabel(player->getName().c_str());
  adder.addLabel(L"Type: ", type.c_str());
  adder.addLabel(L"Rank: ", title.c_str());
  adder.addLabel(L"Fame: ", player->getFame());
  adder.addSpace();
  adder.addLabel(L"Battle Statistics: ");
  winLabel = adder.addLabel(L"Win: ", player->win());
  loseLabel = adder.addLabel(L"Lose: ", player->lose());


  mgr->getChatBox()->setVisible(true);
}

void CStateMainMenu::close()
{
  parent->remove();
  mgr->getChatBox()->setVisible(false);
}

void CStateMainMenu::run()
{
  driver->beginScene(true, true, SColor(255,0,0,0));
  smgr->drawAll();

  env->drawAll();
}


void CStateMainMenu::enable()
{
  parent->setVisible(true);
  mgr->getChatBox()->setVisible(true);
  mgr->getNetworkManager()->setToServer(true);

  stringw win(mgr->getPlayer()->win());
  stringw lose(mgr->getPlayer()->lose());
  winLabel->setText(win.c_str());
  loseLabel->setText(lose.c_str());
}

void CStateMainMenu::disable()
{
  parent->setVisible(false);
}

//! Event Receiving Methods
void CStateMainMenu::OnClientPacketReceived(Packet *packet)
{
  switch (packet->data[0])
  {

    /// Default RakNet Messages
    case ID_CONNECTION_REQUEST_ACCEPTED:
      std::cout << "Successfully joined game room." << std::endl;
      mgr->pushState(new CStateRoom(mgr));
      break;

    case ID_CONNECTION_ATTEMPT_FAILED:
      std::cout << "Failed to join game room." << std::endl;
      break;

    case ID_NO_FREE_INCOMING_CONNECTIONS:
      mgr->getChatBox()->addText(L"The game is full.", true);
      std::cout << "The game is full." << std::endl;
      break;

    case ID_REMOTE_NEW_INCOMING_CONNECTION:
      std::cout << "A player has joined the game." << std::endl;
      break;

    case ID_REMOTE_DISCONNECTION_NOTIFICATION:
    case ID_REMOTE_CONNECTION_LOST:
      std::cout << "A player has left the game." << std::endl;
      break;

    case ID_DISCONNECTION_NOTIFICATION:
      if (mgr->getNetworkManager()->IsHost()){
        std::cout << "A client has disconnected." << std::endl;
      } else {
        std::cout << "You have been disconnected." << std::endl;
      }
      break;

    case ID_CONNECTION_LOST:
      if (mgr->getNetworkManager()->IsHost()){
        std::cout << "A client lost the connection." << std::endl;
      } else {
        std::cout << "Connection lost." << std::endl;
      }
      break;
    default:
      std::cout << "Message with identifier " << int (packet->data[0]) << " has arrived." << std::endl;
      break;
  }
}

void CStateMainMenu::OnServerPacketReceived(Packet *packet)
{

}

//! Called when mouse is clicked
bool CStateMainMenu::OnMouseClick()
{
  return false;
}

//! Called when a GUI button
bool CStateMainMenu::OnButtonClick(s32 id)
{
  CState * room;
  switch (id)
  {
    case BTN_CREA_GAME:
      //! Start game
      //StartGame(true);
      mgr->getNetworkManager()->createRoom();
      mgr->pushState(new CStateRoom(mgr));
      return true;
      break;

    case BTN_JOIN_GAME:
      //! Join game
      //StartGame(false);
      mgr->getNetworkManager()->joinRoom(txtIpAddress->getText());
      cout << "Connecting..." << endl;
      return true;
      break;

    case BTN_TEMPO_BTN:
      //room = new CStateRoom(mgr);
      //mgr->pushState(room);
      mgr->getNetworkManager()->createRoom();

      StartGame(true);

      return true;
      break;

    case BTN_GRAVEYARD:
      room = new CStateGraveyard(mgr);
      mgr->pushState(room);
      break;

    case BTN_EXIT_GAME:
      //! Exit game
      mgr->popState();
      BitStream bs;
      bs.Write(u8(ID_LON_LOGOUT));
      mgr->getNetworkManager()->sendPacket(&bs);
      return true;
      break;
  }

  return false;
}

void CStateMainMenu::StartGame(bool isServer)
{
  cout << "Temporary disabled" << endl;

  CMinionNode *temp;
  CStateGame * game = new CStateGame(mgr);
  game->setBattle(new CBattle());
  game->getBattle()->addPlayer(mgr->getPlayer());

  mgr->getPlayer()->setID(0);
  mgr->getPlayer()->setTeamColor(SColor(255, 255, 0, 0));

  mgr->pushState(game);

  for ( int i=0; i<10; i++ )
  {
    int x = rand() % 512;
    int z = rand() % 512;

    minionStats& stat =  CMinion::minion_base[i%9+1];

    temp = game->getBattle()->addMinion(game, stat, 0);
    temp->getStats().name = stat.typeName;
    // temporary hack
    temp->getStats().hp = temp->getStats().maxHp;
    temp->getStats().mp = temp->getStats().maxMp;
    if (temp)
    {
      temp->prepareNode();
      temp->setPosition(x, z);
    }
  }
  game->getGUI()->updateDelay(game->getBattle()->getMinionList());
}
